package com.czh.subject.application.controller;

import com.czh.subject.application.convert.SubjectCategoryConverter;
import com.czh.subject.application.convert.SubjectLabelConverter;
import com.czh.subject.application.dto.SubjectCategoryDTO;
import com.czh.subject.common.entity.ResponseResult;
import com.czh.subject.common.enums.CategoryTypeEnum;
import com.czh.subject.common.enums.ResponseResultEnum;
import com.czh.subject.domain.entity.SubjectCategoryBO;
import com.czh.subject.domain.service.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * 刷题Controller
 * @author czh01
 * @version 1.0
 * @since 2024/7/23 上午 12:40
 */
@RestController
@RequestMapping("/subject/category")
@Log4j2
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(),"分类名称不能为空!");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空!");
            Preconditions.checkNotNull(subjectCategoryDTO.getImageUrl(),"图标不能为空!");
            SubjectCategoryBO subjectCategoryBo = SubjectCategoryConverter.INSTANCE.convert(subjectCategoryDTO);

            return ResponseResult.success(subjectCategoryDomainService.addSubjectCategory(subjectCategoryBo));
        } catch (Exception e) {
            return ResponseResult.error(ResponseResultEnum.FAILED);
        }
    }

    /**
     *查询所有一级分类
     * @since 2024/7/29 下午 11:01
     */
    @PostMapping("/queryPrimaryCategory")
    public ResponseResult<List<SubjectCategoryDTO>> queryPrimaryCategory() {
        try {
            SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
            subjectCategoryBO.setCategoryType(CategoryTypeEnum.PRIMARY.getCode());
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            return ResponseResult.success(SubjectCategoryConverter.INSTANCE.convert(subjectCategoryBOList));
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return ResponseResult.error(ResponseResultEnum.FAILED);
        }
    }
    /**
     * 根据一级分类查询二级分类
     * @since 2024/7/29 下午 11:30
     */
    @PostMapping("/queryCategoryByPrimary")
    public ResponseResult<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"一级分类ID不能为空!");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"类型不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConverter.INSTANCE.convert(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            return ResponseResult.success(SubjectCategoryConverter.INSTANCE.convert(subjectCategoryBOList));
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return ResponseResult.error(ResponseResultEnum.FAILED);
        }
    }
    /**
     * (优化)查询分类和标签
     * @since 2024/9/4 下午 11:01
     */
    @PostMapping("/queryCategoryAndLabel")
    public ResponseResult<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"一级分类ID不能为空!");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConverter.INSTANCE.convert(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryAndLabelOptimize2(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = new LinkedList<>();
            subjectCategoryBOList.forEach(boList->{
                SubjectCategoryDTO subjectCategoryDTOTemp = SubjectCategoryConverter.INSTANCE.convert(boList);
                subjectCategoryDTOTemp.setLabelDTOList(SubjectLabelConverter.INSTANCE.convert(boList.getSubjectLabelBOList()));
                subjectCategoryDTOList.add(subjectCategoryDTOTemp);
            });
            return ResponseResult.success(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return ResponseResult.error(ResponseResultEnum.FAILED);
        }
    }
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConverter.INSTANCE.convert(subjectCategoryDTO);
            return ResponseResult.success(subjectCategoryDomainService.update(subjectCategoryBO));
        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}", e.getMessage(), e);
            return ResponseResult.error("更新分类失败!");
        }
    }
    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConverter.INSTANCE.convert(subjectCategoryDTO);
            return ResponseResult.success( subjectCategoryDomainService.delete(subjectCategoryBO));
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return ResponseResult.error("删除分类失败!");
        }
    }
}
